const DYNAMIC_TYPE = {
    "1": "year",
    "2": "column",
    "3": "table", //试算平衡表
};
/**动态参数类型 */
const DYNAMIC_PARAM = {
    "1": "default",
    "2": "parameter", //带参数
};
/**
 * 判断是否时动态公式 tname or cellMeta 两者有一个时必选
 * @param type: 表类型 ds|fs|rs(报表基础类型自定义)
 * @param col: 行号 type是fs，必选
 * @parma cellMeta: type是ds时，必选
 * @param rowData; type是rs时，必选
 *        所属报表是资产负债表，审定数的这列才是动
 *        所属报表是利润表，本期审定发生额这一列才是动
 */
function isDynamicFormula(type, col, cellMeta, rowData) {
    //选中附注期间类型的单元格
    if (type === "ds" && cellMeta && cellMeta?.timeColumn) {
        return true;
    }
    //财务报表从第二列开始都是动态类型
    if (type === "fs" && col && +col > 1) {
        return true;
    }
    //报告基础数据,自定义的类型rs，只有在此处生效
    if (type === "rs") {
        const { report, caption } = rowData;
        if (report === "资产负债表" &&
            (caption === "审定数" || caption === "期末审定数")) {
            return true;
        }
        if (report === "利润表" && caption === "本期审定发生额") {
            return true;
        }
    }
    return false;
}
/**
 * 查找字符串的第N个参数,并删除QC和年度
 * @param str
 * @returns
 */
function delQcAndYear(str) {
    // 使用正则表达式和字符串方法查找第三个参数
    if (str) {
        str = str.toString();
        return str.replace(/^(QC\d{4,8}|QC|\d{4,8}QC|\d{4,8})/, "");
    }
    return str;
}
/**
 * 删除试算平衡表中的年度和期间
 */
function delTnameAndYear(str) {
    if (str.includes("_")) {
        const arr = str.split("_");
        return arr[1];
    }
    return str;
}
/**
 * 动态公式替换
 * @param params 动态类型
 */
export function dynamicFormulaReplace(params) {
    const { dynamicType, paramType, type, formulaData, col, cellMeta, rowData, dynamicCondition, } = params;
    if (!dynamicCondition) {
        return;
    }
    const isDynamic = isDynamicFormula(type, col, cellMeta, rowData);
    console.log(formulaData, "formulaData----", isDynamic, params);
    if (!isDynamic) {
        return;
    }
    const { tname, rowVal, colVal, year } = formulaData;
    //报表基础数据 year类型： 默认参数回填
    if (dynamicType === DYNAMIC_TYPE["1"] && paramType === DYNAMIC_PARAM["1"]) {
        // const val = `FS_CELL("base.reportItems","${tname}","{${rowVal}}", "{year}")`;
        const val = `FS_CELL("${tname}","${rowVal}","{${colVal}}", "{year}")`;
        return val;
    }
    //附注数据 column类型
    if (dynamicType === DYNAMIC_TYPE["2"] && type === "ds") {
        // const val = `CP_CELL("${tname}","${rowVal}","{金额}","${year}")`;
        const newColVal = delQcAndYear(colVal);
        const val = `CP_CELL("${tname}","${rowVal}","{${newColVal}}","${year}")`;
        return val;
    }
    //财务报表数据 column类型
    if (dynamicType === DYNAMIC_TYPE["2"] && type === "fs") {
        console.log("财务报表数据 column类型 ");
        const newColVal = delQcAndYear(colVal);
        const val = `CP_CELL("${tname}","${rowVal}","{${newColVal}}")`;
        return val;
    }
    //试算平衡表 table类型
    if (dynamicType === DYNAMIC_TYPE["3"]) {
        const newTname = delTnameAndYear(tname);
        const val = `CP_CELL("{${newTname}}","${rowVal}","${colVal}")`;
        return val;
    }
}
/**
 * incell动态公式替换
 * @param dynamicCondition: 是否开启批量
 * @param parentType: 父type
 * @param parentName : 父name
 * @param rowVal :row val
 * @param colVal : col val
 * @returns
 */
export function incellDynamicFormulaReplace(dynamicCondition, parentType, parentName, rowVal, colVal, cellMeta, col) {
    parentType = parentType === "zl" ? "ds" : parentType;
    if (!dynamicCondition) {
        return;
    }
    //勾选批量的情况下,附注单个公式变成四个格公式
    const isDynamic = isDynamicFormula(parentType, col, cellMeta);
    if (parentType === "ds" && !isDynamic) {
        return `IN_CELL("本表", "${rowVal}","${colVal}", "${parentType}")`;
    }
    if (!isDynamic) {
        return;
    }
    //附注和非财务报表为column类型
    if (parentType === "ds" ||
        (parentType === "fs" && !parentName.includes("试算"))) {
        // const newColVal =
        //   parentType === "fs" ? `{${delQcAndYear(colVal)}}` : `{金额}`;
        const newColVal = `{${delQcAndYear(colVal)}}`;
        return `IN_CELL("本表", "${rowVal}","${newColVal}", "${parentType}")`;
    }
    //试算财务报表为column类型
    if (parentType === "fs" && parentName.includes("试算")) {
        const newTname = delTnameAndYear(parentName);
        return `IN_CELL("{${newTname}}", "${rowVal}","${colVal}", "${parentType}")`;
    }
}
